package com.nicolasprof.profgallery.effects {

	import com.nicolasprof.profgallery.Engine;
	/**
	 * @author Nicolas Prof
	 */
	public final class EffectManager extends Object {
		
		private static var $instance:EffectManager = new EffectManager();
		public static const EFFECTS:Object = {
												'fade' : AlphaEffect, 
												'bricks' : BricksEffect, 
												'doors' : DoorsEffect, 
												'fadeWave' : FadeWaveEffect, 
												'flare' : FlareEffect, 
												'wiper' : WiperEffect, 
												'zoom' : ZoomEffect
											};

		private var _rotator : Array;
		private var _cursor : int;

		public function EffectManager() {
			if( $instance ) throw new Error( "Singleton and can only be accessed through Singleton.getInstance()" );
		}
		public static function getInstance():EffectManager {
			return $instance;
		}
		private function buildRotator() : void {
			_rotator = [];
			var config: XML = Engine.CONFIG_XML;
			var effects : Array = String(config..effects.@type[0]).split(",");
			var i: int;
			var len: int = effects.length;
			for(i=0; i < len; i++) {
				if(effects[i] == "all"){
					for each( var effect : * in EffectManager.EFFECTS){
						_rotator.push(effect);
					}
				}else{
					_rotator.push(EffectManager.EFFECTS[effects[i]]);
				}
			}
		}

		public function getEffect() : Effect {
			if(_rotator == null){
				buildRotator();
			}
			if(++_cursor >= _rotator.length) {
				_cursor = 0;
			}		
			return new _rotator[_cursor]();
		}
	}
}
